const WXAPI = require('../../Wxapi/api.js');
const util = require('../../utils/util.js');

var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow: true, //无活动内容时显示
    top_img: '',
    part_list: false,

    cdn_path: app.globalData.cdnRoot,
    page: 1,
    limit: 10,
    append: false,
    is_over: false,
    search: '',
    datetimeTo: "", // 开始时间
    timeLeft: "",    // 剩下的时间（天时分秒）
    uv:'-',
    voter: '-',
    votes: '-',
    errorMsg: ""
  },
  onShow: function () {
    this.data.timer = setInterval(() => { //注意箭头函数！！
      this.setData({
        timeLeft: util.getTimeLeft(this.data.datetimeTo)//使用了util.getTimeLeft
      });
      if (this.data.timeLeft === false) {
        this.setData({
          timeLeft: '已结束'
        });
      }
    }, 1000);
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (options.which){
      wx.showLoading({
        title: '加载中...',
        mask: true
      })
      if (options.which === "video"){
        setTimeout(function(){
          wx.navigateTo({
            url: '/pages/video/video?id=' + options.id,
          })
          wx.hideLoading()
        }, 1000)
      }
    }
    
    // 获取活动数据
    that.getActiveInfo(1)

    // 获取参与者列表
    that.getVideoList(1, that.data.page, that.data.limit, that.data.search)

    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      top_img: '',
      part_list: false,

      cdn_path: app.globalData.cdnRoot,
      page: 1,
      limit: 10,
      append: false,
      is_over: false,
      search: ''
    })
    this.onLoad([])
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this,
        page = that.data.page + 1,
        limit = that.data.limit,
        search = that.data.search;
    // 获取活动数据
    that.getVideoList(1, page, limit, search)    
  },

  
  onTapdetail: function (event) {
    let that = this
    wx.navigateTo({
      url: '/pages/video/video?id=' + event.currentTarget.dataset.id  //跳转详情页 
    })
  },
  getActiveInfo: function (id){
    let that = this
    WXAPI.getActiveInfo({
      id: id
    }).then(function (res) {
      if (res.code === 200) {
        that.setData({
          isShow: true
        });
        // 设置活动信息缓存数据
        wx.setNavigationBarTitle({
          title: res.data.title  //修改title
        })
        wx.setStorageSync("activeInfo", res.data)
        that.setData({
          top_img: app.globalData.cdnRoot + res.data.top_img,
          datetimeTo: res.data.end_time,
          uv: res.uv.uv,
          voter: res.uv.voter,
          votes: res.uv.votes
        })
      } else {
        that.setData({
          isShow: false,
          errorMsg: res.errorMsg
        });
      }
    })
  },
  getVideoList: function (id, page, limit, search){
    let that = this,
        append = that.data.append,
        list = that.data.part_list; 
    wx.showLoading({
      title: '拼命加载中...'
    })
    WXAPI.getVideoList({
      id:id,
      page:page,
      limit: limit,
      search: search
    }).then(function(res){
      if(append === false){
        if (res.code === 200) {
          list = res.list;
          that.setData({
            append: true,
            part_list: list
          })
        }
      } else {
        if(res.code === 200){
          for(let i in res.list){
            list.push(res.list[i])
          }
          that.setData({
            part_list: list,
            is_over: false,
            page: page
          })
        } else {
          if(list.length > 0){
            that.setData({
              is_over: true
            })
          }
        }
      }
    }).finally(function(){
      wx.hideLoading()
    })
  },
  formSubmit: function(e){
    let that = this,
        search = e.detail.value.search,
        limit = that.data.limit;
    if (search === '' || search === null){
      wx.showToast({
        title: '请输入关键词',
        duration: 1500,
        icon: "none"
      })
    } else {
      that.setData({
        search: search,
        part_list: false,
        is_over: false,
        append: false,
        page: 1
      })
      that.getVideoList(1, 1, limit, search)
    }
  },
  goLoginPageTimeOut: function () {
    setTimeout(function () {
      wx.navigateTo({
        url: '/pages/authorize/index'
      })
    }, 1000)
  }
})